package com.example.project1

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.project1.ui.theme.Project1Theme

class MainActivity : ComponentActivity() {
    val TAG = "MainActivity"

    private val getContent = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
        //处理收到内容
        Log.d(TAG, "Result callback...")
        it.data?.apply {
            Log.i(TAG, "returned data: ${this.getStringExtra("result")}")
        }
    }

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //TAG表示一个分组，用于打印日志
        Log.d(TAG, "On created...")
        setContentView(R.layout.activity_main)

        //按钮事件
        val btn = findViewById<Button>(R.id.button)

        btn.setOnClickListener {
            Log.i(TAG, "On click button.")

            //意图，::调用Java的class对象
            val intent = Intent(this, SecondActivity::class.java)

            //传送数据
            intent.putExtra("name", "Jack")
            /artActivity(intent)

            //显示回调信息
            /artActivityForResult(intent, SecondActivity.RES_CODE)
            getContent.launch(intent)
        }
    }

}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    Project1Theme {
        Greeting("Android")
    }
}